package com.day0923.shopping.manager;

import com.day0923.shopping.data.Customer;
import com.day0923.shopping.data.Date;

import java.util.Arrays;
import java.util.Scanner;

public class CustManager {
    Scanner in = new Scanner(System.in);
    Date date;
    Menu menu;
    //显示会员信息
    public void custManagerShow(){
        System.out.println("  会员号            生日             积分");
        System.out.println("------------|------------|---------------");
        for (int i = 0; i < date.getCustomers().length; i++) {
            int c_no = date.getCustomers()[i].getCustNo();
            String c_birth = date.getCustomers()[i].getCustBirth();
            int c_score = date.getCustomers()[i].getCustScore();
            System.out.println(c_no + "\t\t" + c_birth + "\t\t" + c_score);
        }
        returnLastMenu();
    }

    /**
     * 返回上一级菜单
     */
    public void returnLastMenu() {
        System.out.print("\n\n请按'n'返回上一级菜单:");
        Scanner input = new Scanner(System.in);
        boolean con = true;
        do {
            if (input.next().equals("n")) {
                menu.showMainMenus();
            } else {
                System.out.print("输入错误, 请重新'n'返回上一级菜单：");
                con = false;
            }
        } while (!con);
    }

//    添加会员
    public void custManagerAdd(){
        String answer;
        do {
            Customer[] newCunsomers = Arrays.copyOf(date.getCustomers(),date.getCustomers().length+1);
            date.setCustomers(newCunsomers);
            System.out.print("请输入会员号：");
            int c_no = in.nextInt();
            System.out.print("请输入会员生日：");
            String c_birth = in.next();
            System.out.print("请输入会员积分：");
            int c_score = in.nextInt();
            Customer customer = new Customer(c_no,c_birth,c_score);
            date.getCustomers()[date.getCustomers().length-1] = customer;
            System.out.println("会员添加成功！");
            System.out.println("要继续添加嘛（y/n）");
            answer = in.next();
        }while ("y".equals(answer));
        returnLastMenu();
    }

//  修改客户信息
    public void custManagerRevise(){
        System.out.print("请输入会员卡号：");
        int c_no = in.nextInt();
        System.out.println("  会员号            生日             积分      ");
        System.out.println("------------|------------|---------------");
        boolean flag = false;
        int index = 0;
        for (int i = 0; i < date.getCustomers().length; i++) {
            if (date.getCustomers()[i].getCustNo() == c_no) {
                System.out.println(date.getCustomers()[i].getCustNo() + "\t\t" + date.getCustomers()[i].getCustBirth() + "\t\t" + date.getCustomers()[i].getCustScore());
                flag = true;
                index = i;
                break;
            }
        }
        if (flag){
            System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
            System.out.println("\t\t\t\t1.修 改 会 员 生 日");
            System.out.println("\t\t\t\t2.修 改 会 员 积 分");
            System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
            System.out.print("请选择，输入数字：");
            int choose_num = in.nextInt();
            String answer = null;
            do {
                if (choose_num == 1) {
                    System.out.print("请输入修改后的会员生日：");
                    String c_birth = in.next();
                    date.getCustomers()[index].setCustBirth(c_birth);
                    System.out.println("修改成功！");
                    System.out.print("是否修改其他属性（y/n）:");
                    answer = in.next();
                }
                else if (choose_num == 2){
                    System.out.print("请输入修改后的会员积分：");
                    String c_score = in.next();
                    date.getCustomers()[index].setCustBirth(c_score);
                    System.out.println("修改成功！");
                    System.out.print("是否修改其他属性（y/n）:");
                    answer = in.next();
                }
            }while ("y".equals(answer));
        }
        else {
            System.out.println("对不起，没有该会员号！");
        }
        returnLastMenu();
    }

    //查 询 客 户 信 息
    public void custManagerSelect(){
        System.out.print("请输入会员卡号：");
        int c_no = in.nextInt();
        boolean flag = false;
        for (int i = 0; i < date.getCustomers().length; i++) {
            if (date.getCustomers()[i].getCustNo() == c_no) {
                System.out.println("  会员号            生日             积分      ");
                System.out.println("------------|------------|---------------");
                System.out.println(date.getCustomers()[i].getCustNo() + "\t\t" + date.getCustomers()[i].getCustBirth() + "\t\t" + date.getCustomers()[i].getCustScore());
                flag = true;
                break;
            }
        }
        if(flag == false) {
            System.out.println("抱歉，没有该会员信息！");
        }
    }

}
